﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace DataExporter
{
    class TypeData
    { 
        public string Name{get;set;}
        public int  Count{get;set;}
    }

    class WaveNode
    {
        public string Name{get;set;}
        public List<TypeData> TypeList=new List<TypeData>();
        public List<TypeData> QualityList = new List<TypeData>();
    }

    class FileValue
    {
        public string FileData { get; set; }
        public float QualityValue { get; set; }
        public FileValue(string data, float v)
        {
            FileData = data;
            QualityValue = v;
        }
    };

    class Program
    {
        static int ParseIndex(string str)
        {
            int result = 0;
            try
            {
                int n = str.LastIndexOf("_");


                n = str.LastIndexOf("_", n - 1);
                if (n < 1) return 0;
                string d = str.Substring(n, 1);
                result = int.Parse(d);
            }
            catch (Exception)
            {
                return 0;
            }

            return result;
        }
        static void Main(string[] args)
        {
            if(args.Length<=0)return;
            string file=args[0].Replace("xml","txt");
            List<WaveNode> datalist = new List<WaveNode>();

            //Stack<FileValue> fvs = new Stack<FileValue>();
            SortedList<float, string>[] vvs = new SortedList<float, string>[10];
            for (int i = 0; i < vvs.Length; i++)
                vvs[i] = new SortedList<float, string>();

            SortedList<float,string> fvs=new SortedList<float,string>();

            XmlDocument doc = new XmlDocument();
            doc.Load(args[0]);

            FileStream fs = new FileStream(file, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs,Encoding.Default);
            

            //header
            XmlNodeList nodes = doc.GetElementsByTagName("TestInfo");
            XmlElement info = nodes[0] as XmlElement;
            string infotext = "";
            infotext += info.GetAttribute("Name"); infotext += "\t";
            infotext += info.GetAttribute("UserName"); infotext += "\t";
            infotext += info.GetAttribute("Sex"); infotext += "\t";

            DateTime date = DateTime.Parse(info.GetAttribute("BirthDate"));

            infotext += date.ToString("yyyy/MM/dd"); infotext += "\t";
            infotext += info.GetAttribute("Handedness"); infotext += "\t";
            infotext += info.GetAttribute("EducationalBackground"); infotext += "\t";
            infotext += info.GetAttribute("OccupationOrSpecialty"); infotext += "\t";
            
            TimeSpan sp = DateTime.Now - date;

            if ((sp.TotalDays / 365) > 16)
                infotext += "成人\t";
            else
                infotext += "学生\t";

            infotext += info.GetAttribute("Memo"); infotext += "\t";
            infotext += info.GetAttribute("EvaluationType");
            sw.WriteLine(infotext);

            //heaer

            XmlNodeList root = doc.GetElementsByTagName("wave_file");
            int maxIndex = 0;
            if(root.Count>0)
            {
                float off = 0;
                foreach (XmlNode wavenode in root)
                {
                    off += 0.0001f;

                    string data = "";
                    XmlElement node = wavenode as XmlElement;
                    string Filename = node.GetAttribute("FileName");
                    data += Filename+ "\t";

                    float filevalue = 0;
                    
                    foreach (XmlNode subnode in wavenode.ChildNodes)
                    {
                        XmlElement node2 = subnode as XmlElement;
                        string name=node2.GetAttribute("Name");
                        string typename=node2.GetAttribute("Type");
                        string qualityname = node2.GetAttribute("Quality");
                        filevalue += float.Parse(node2.GetAttribute("Quality_Value"));
                        data += "\t";
                        data += name+":";
                        data += typename ;
                        data += string.Format(":{0}", qualityname)+"\t";

                        WaveNode wNode = null;
                        foreach (WaveNode wdata in datalist)
                        {
                            if (wdata.Name == name)
                            {
                                wNode = wdata;
                                break;
                            }
                        }
                        if (wNode==null)
                        {
                            wNode = new WaveNode();
                            wNode.Name = name;
                            datalist.Add(wNode);
                            foreach (string tl in Properties.Settings.Default.TypeList)
                            {
                                TypeData tt = new TypeData();
                                tt.Name = tl;
                                tt.Count = 0;
                                wNode.TypeList.Add(tt);
                            }
                            foreach (string tl in Properties.Settings.Default.QualityList)
                            {
                                TypeData tt = new TypeData();
                                tt.Name = tl;
                                tt.Count = 0;
                                wNode.QualityList.Add(tt);
                            }
                        }

                        TypeData typeNode =null;
                        foreach (TypeData td in wNode.TypeList)
                        {
                            if (td.Name == typename)
                            {
                                typeNode = td;
                                typeNode.Count++;
                            }
                        }

                        foreach (TypeData td in wNode.QualityList)
                        {
                            if (td.Name == qualityname)
                            {
                                typeNode = td;
                                typeNode.Count++;
                            }
                        }
                    }
                    try
                    {
                        int index = ParseIndex(Filename);
                        maxIndex = Math.Max(index, maxIndex);

                        vvs[index].Add(filevalue + off, data);
                       // fvs.Add(filevalue+off, data);
                        List<int> ss = new List<int>();

                    }
                    catch (Exception eee)
                    {
                    }
                    
                    //sw.WriteLine(data);
                }
            }
           
            if (maxIndex <= 0)
            {
                IList<string> ilistValues = vvs[0].Values;
                IList<float> ilistKeys = vvs[0].Keys;
                float v = 0;
                for (int i = 0; i < 3 && i < ilistValues.Count; i++)
                {
                    v += ilistKeys[i];
                    sw.WriteLine(ilistValues[i]);
                }
                sw.Write("总分:");
                sw.WriteLine(v.ToString());
            }
            else
            {
                float v = 0;
                for (int i = 0; i < maxIndex; i++)
                {
                    IList<string> ilistValues = vvs[i].Values;
                    IList<float> ilistKeys = vvs[i].Keys;
                    
                    for (int j = 0; j < 3 && j < ilistValues.Count; j++)
                    {
                        v += ilistKeys[j];
                        sw.WriteLine(ilistValues[j]);
                    }
                }
                sw.Write("总分:");
                sw.WriteLine(v.ToString());
            }

            foreach (WaveNode node in datalist)
            {
                string data = node.Name;
                data+="\t";
                foreach (TypeData typenode in node.TypeList)
                {
                    data += string.Format("{0}:{1}\t", typenode.Name, typenode.Count);
                }
                foreach (TypeData typenode in node.QualityList)
                {
                    data += string.Format("{0}:{1}\t", typenode.Name, typenode.Count);
                }
                sw.WriteLine(data);
            }
            sw.Close();
            fs.Close();
        }
    }
}
